var eidInfo;

BlobBuilder = window.MozBlobBuilder || window.WebKitBlobBuilder || window.BlobBuilder;

function getInfo(fieldName, fieldFirstName, fieldZipCode, fieldCity){
	document.getElementById('eidloader').innerHTML= '<img src="images/ajax-loader.gif"> Bezig met laden van gegevens...';
	var xhr = new XMLHttpRequest();
	xhr.open('GET', 'http://localhost:8081/eid', true);
	
	xhr.onload = function(e) {
	  if (this.status == 200) {
		  document.getElementById('eidloader').innerHTML='';
		  try{
			  var eidInfo = eval('(' + this.responseText + ')');
			  document.getElementById(fieldName).innerHTML='<b>'+ eidInfo.name + '<b>';
			  document.getElementById(fieldFirstName).innerHTML='<b>'+ eidInfo.firstName + '<b>';
			  document.getElementById(fieldZipCode).innerHTML='<b>'+ eidInfo.zipcode + '<b>';
			  document.getElementById(fieldCity).innerHTML='<b>'+ eidInfo.city + '<b>';
			  getPhoto();
		  }catch(err){
			  alert("Fout bij lezen van eID. Is er een kaart aanwezig?");
		  }
	  }
	};

	xhr.send();
	}
	
function getPhoto(){
	document.getElementById('eidloader').innerHTML= '<img src="images/ajax-loader.gif"> Bezig met laden van foto...';
	var xhr = new XMLHttpRequest();
	xhr.open('GET', 'http://localhost:8081/eid/photo', true);
	xhr.responseType = 'arraybuffer';
	
	xhr.onload = function(e) {
	  if (this.status == 200) {
		  document.getElementById('eidloader').innerHTML='';
		  try{
			var bb = new BlobBuilder();
		    bb.append(this.response); // Note: not xhr.responseText
		    var blob = bb.getBlob('image/jpeg');
		    
		  	var img = document.getElementById('eidphoto');
		    img.onload = function(e) {
		      window.webkitURL.revokeObjectURL(img.src); // Clean up after yourself.
		    };
		    img.src = window.webkitURL.createObjectURL(blob);
		    
	        document.getElementById('eidloader').innerHTML='';
		  }catch(err){
			  alert("Fout bij lezen van eID. Is er een kaart aanwezig?");
		  }
	  }
	};

	xhr.send();
	}



